package com.example.ccj.b.c.custom;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;

import com.example.ccj.R;
import com.example.ccj.b.c.custom.adapter.LittleImageAdapter;
import com.example.ccj.b.c.recycler.BaseRecycler;
import com.example.ccj.u.ScreenUtils;
import com.example.ccj.u.SizeUtils;
import com.example.ccj.u.StringUtils;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

/**
 * 横向多行加载小图片控件
 * 根据屏幕尺寸决定每行加载多少个图片
 */
public class ShowLittleImageListControl extends LinearLayout {
    List<LocalMedia> pictureList;
    LinearLayout mLlTitle;
    TextView mTvTitleName;
    BaseRecycler<LocalMedia> mRecAddImg;

    public ShowLittleImageListControl(Context context) {
        super(context);
        init();
    }
    public ShowLittleImageListControl(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void init(){
        LayoutInflater.from(getContext()).inflate(R.layout.control_show_little_image,this);
        pictureList = new ArrayList<>();

        mLlTitle = findViewById(R.id.control_show_little_image_title);
        mTvTitleName = findViewById(R.id.control_show_little_image_title_name);
        mRecAddImg = findViewById(R.id.control_show_little_image_rec);

        //获取屏幕宽度根据宽度设置每行加载的图片数量
        int appScreenWidth = ScreenUtils.getAppScreenWidth();
        int dpWidth = SizeUtils.px2dp(appScreenWidth);
        int i = dpWidth / 40;
        mRecAddImg.setLayoutManager(new GridLayoutManager(getContext(), i));
    }

    /**
     * 显示头部
     * @param name 名称
     */
    public void showTitle(String name){
        if (StringUtils.isEmpty(name)) mTvTitleName.setText("图片选择控件");
        else  mTvTitleName.setText(name);
        mLlTitle.setVisibility(VISIBLE);
    }

    public List<LocalMedia> getPictureList() {
        return pictureList;
    }

    public ShowLittleImageListControl setPictureList(List<LocalMedia> pictureList) {
        this.pictureList = pictureList;
        mRecAddImg.setAdapter(pictureList,new LittleImageAdapter(pictureList));
        return this;
    }

    public LinearLayout getmLlTitle() {
        return mLlTitle;
    }

    public TextView getmTvTitleName() {
        return mTvTitleName;
    }

    public BaseRecycler<LocalMedia> getmRecAddImg() {
        return mRecAddImg;
    }
}
